using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Weathertastic
{
    public partial class SettingsForm : Form
    {
        private Settings Settings;

        public SettingsForm()
        {
            InitializeComponent();
            this.Icon = Icon.FromHandle(((Bitmap)Image.FromFile("images/settings.png")).GetHicon());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            Settings = new Settings();
            Settings.UseImperial = cbUseImperial.Checked;
            Settings.Theme = comboBox1.SelectedItem.ToString();
            Settings.SaveSettings();
            this.Close();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            Settings = new Settings();

            // Load locations
            listView1.Items.Clear();
            Locations locs = new Locations();
            foreach(Location loc in locs)
            {
                ListViewItem item = new ListViewItem(loc.Name);
                item.Tag = loc.Station;
                if (loc.Default)
                    item.Font = new Font(item.Font, item.Font.Style | FontStyle.Bold);
                listView1.Items.Add(item);
            }

            // Load themes
            comboBox1.Items.Clear();
            int iSelectedTheme = -1;
            int i = 0;
            foreach (string d in Directory.GetDirectories("icons\\"))
            {
                if (File.Exists(d + "\\theme.xml"))
                {
                    comboBox1.Items.Add(d.Replace("icons\\", ""));
                    if (d.Replace("icons\\", "").Equals(Settings.Theme))
                        iSelectedTheme = i;

                    i++;
                }
            }

            if (iSelectedTheme > -1)
                comboBox1.SelectedIndex = iSelectedTheme;

            // Assign unit checbox
            cbUseImperial.Checked = Settings.UseImperial;


            Settings = null;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            LocationPicker lp = new LocationPicker();
            lp.ShowDialog();
            SettingsForm_Load(this, null); // Reload location data

        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                Locations locs = new Locations();
                foreach (ListViewItem item in listView1.SelectedItems)
                {
                    locs.Remove(item.Tag);
                    listView1.Items.Remove(item);
                }
                locs = null;
            }
        }

        private void btnSetDefault_Click(object sender, EventArgs e)
        {
            Locations locs = new Locations();
            locs.SetDefault(listView1.SelectedItems[0].Tag.ToString());
            SettingsForm_Load(this, null); // Reload location data
        }
    }
}